How to swap or move 2 string in Array? [on hold]

Posted by Wisnu Khazefa on Programmers See other posts from Programmers or by Wisnu Khazefa
Published on 2014-08-22T10:51:55Z Indexed on 2014/08/22 16:35 UTC
Read the original article Hit count: 132

Filed under:
|

I have a need to convert .csv file to .dat file. In my problem, there are value pairs, with a name attribute (called Fund) and corresponding numeric value. If the input file has a pair whose value is 0, then that pair (Fund and value) is dropped. The output file should have only those pairs (Fund and value) where the value is non-zero.

Here is the prototype of my code.

public static void Check_Fund(){
String header = "Text1,Text2,Text3,FUND_UALFND_1,FUND_UALPRC_1,FUND_UALFND_2,"
               +"FUND_UALPRC_2,FUND_UALFND_3,FUND_UALPRC_3,FUND_UALFND_4,FUND_UALPRC_4,FUND_UALFND_5,FUND_UALPRC_5,Text4,Text5,Text6,Text7";
String text = "ABC;CDE;EFG;PRMF;0;PRFF;50;PREF;0;PRCF;0;PRMP;50;TAHU;;BAKWAN;SINGKONG";
String[] head;
String[] value;
String showText = "";
head = header.split(",");
value = text.split(";");

String regex = "\\d+";  

String[] fund = {"PREF","PRMF","PRFF","PRCF","PRMP","PDFF","PSEF","PSCB","PSMF","PRGC","PREP"};
for(int i = 0; i < value.length; i++){
    for(int j=0;j < fund.length; j++){
        if(value[i].equals(fund[j]) && value[i+1].matches(regex)){
           if(value[i+1].equals("0")){
              value[i] = "";
              value[i+1] = "";
           }
        }
    }
    showText = showText + head[i] +":" + value[i] + System.lineSeparator();
}
System.out.println(showText );
}

Expected Result

Input:

FUND_UALFND_1:PRMF
FUND_UALPRC_1:0
FUND_UALFND_2:PRFF
FUND_UALPRC_2:50
FUND_UALFND_3:PREF
FUND_UALPRC_3:0
FUND_UALFND_4:PRCF
FUND_UALPRC_4:0
FUND_UALFND_5:PRMP
FUND_UALPRC_5:50

Output:

FUND_UALFND_1:PRFF
FUND_UALPRC_1:50
FUND_UALFND_2:PRMP
FUND_UALPRC_2:50
FUND_UALFND_0:
FUND_UALPRC_0:
FUND_UALFND_0:
FUND_UALPRC_0:
FUND_UALFND_0:
FUND_UALPRC_0:

© Programmers or respective owner

Related posts about java

Related posts about array